package com.kolon.business.mOI.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jdom.Document;
import org.jdom.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.kolon.framework.constant.Constant;
import com.kolon.framework.interfaces.SimpleController;
import com.kolon.framework.util.ComUtil;
import com.kolon.framework.util.MsgUtil;

@Controller("com.kolon.business.mOI.controller.MOITypeEffectController")
public class MOITypeEffectController implements SimpleController {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value="/business/mOI/mOITypeEffect.do")
	public ModelAndView controller(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// XML URL
		String url = MsgUtil.getMessage("xml.url");
		// 세션
		HttpSession httpSession = request.getSession();
		Map userMap = (Map)httpSession.getAttribute(Constant.SESSION_USER_INFO);
		String userId = (String)userMap.get("userId");
		String userPw = (String)userMap.get("userPw");
		String cpyCD = (String)userMap.get("CompanyCD");
		StringBuffer buffer = new StringBuffer();
		buffer.append(url); 
		buffer.append("/oi_suggestion.aspx?strQuery=Y&strGubun=Effect&companycd=").append(cpyCD);
		// XML DOCUMENT 및 인증 처리
		Document doc = ComUtil.getDocument(buffer.toString(), userId, userPw);
		Element element = doc.getRootElement();
		List xmlList = element.getChildren("Table");
		Iterator itXml = xmlList.iterator();
		Map gbMap = null;
		List gbList = new ArrayList();
		// XML 데이터  (나의 제안 수)
		while (itXml.hasNext()) {
			gbMap = new HashMap();
			Element el = (Element)itXml.next();
			gbMap.put("UCODE", el.getChild("UCODE").getText());
			gbMap.put("GBNAME", el.getChild("GBNAME").getText());
			gbMap.put("SUB1", el.getChild("SUB1").getText());
			gbMap.put("SUB2 ", el.getChild("SUB2").getText());
			gbMap.put("CompanyCD", el.getChild("CompanyCD").getText());
			gbList.add(gbMap);
		}
		// 리턴
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("gbList", gbList);
		return modelAndView;
	}

}
